package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.yanq.liancar.R;
import com.yanq.liancar.bean.MileageBBean;
import com.yanq.liancar.bean.OdbDriverActBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Utils;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//驾驶行为
public class DrivingBehaviorActivity extends AppCompatActivity {

    @BindView(R.id.back_btn)
    ImageView backBtn;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.driveBehavior_cheshou)
    TextView driveBehaviorCheshou;
    @BindView(R.id.driveBehavior_jihui)
    TextView driveBehaviorJihui;
    @BindView(R.id.driveBehavior_chepai)
    TextView driveBehaviorChepai;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.tv_licheng)
    TextView tvLicheng;
    @BindView(R.id.tv_shichang)
    TextView tvShichang;
    @BindView(R.id.tv_youhao)
    TextView tvYouhao;
    @BindView(R.id.tv_chaosu)
    TextView tvChaosu;
    @BindView(R.id.tv_jijiasu)
    TextView tvJijiasu;
    @BindView(R.id.tv_jishache)
    TextView tvJishache;
    @BindView(R.id.view1)
    View view1;
    @BindView(R.id.ll_benci)
    LinearLayout llBenci;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.ll_dangri)
    LinearLayout llDangri;
    @BindView(R.id.view3)
    View view3;
    @BindView(R.id.ll_benzhou)
    LinearLayout llBenzhou;
    @BindView(R.id.view4)
    View view4;
    @BindView(R.id.ll_benyue)
    LinearLayout llBenyue;

    private int type = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driving_behavior);
        ButterKnife.bind(this);
        Utils.setStatusBar(this, false, true);
        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initGetMileage(type);
    }


    @OnClick({R.id.driveBehavior_jihui, R.id.ll_benci, R.id.ll_dangri, R.id.ll_benzhou, R.id.ll_benyue})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.driveBehavior_jihui://去抽奖
                Bundle bundle=new Bundle();
                bundle.putInt("where",1);
                RxActivityTool.skipActivity(this, SmallCLuckDrawActivity.class,bundle);
                break;
            case R.id.ll_benci:
                type = 1;
                initGetMileage(type);
                view1.setVisibility(View.VISIBLE);
                view2.setVisibility(View.GONE);
                view3.setVisibility(View.GONE);
                view4.setVisibility(View.GONE);
                break;
            case R.id.ll_dangri:
                type = 2;
                initGetMileage(type);
                view1.setVisibility(View.GONE);
                view2.setVisibility(View.VISIBLE);
                view3.setVisibility(View.GONE);
                view4.setVisibility(View.GONE);
                break;
            case R.id.ll_benzhou:
                type = 3;
                initGetMileage(type);
                view1.setVisibility(View.GONE);
                view2.setVisibility(View.GONE);
                view3.setVisibility(View.VISIBLE);
                view4.setVisibility(View.GONE);
                break;
            case R.id.ll_benyue:
                type = 4;
                initGetMileage(type);
                view1.setVisibility(View.GONE);
                view2.setVisibility(View.GONE);
                view3.setVisibility(View.GONE);
                view4.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void initGetMileage(int i) {
        OkGo.<String>post(Config.s + "llc/odbcheck/queryOdbDriverAct")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .params("type", i)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        OdbDriverActBean info = gson.fromJson(response.body(), OdbDriverActBean.class);
                        if (info.isIsSuccess()) {
                            if (info.getData()!=null){
                                tvNum.setText(info.getData().getScore() + "");
                                tvJijiasu.setText(info.getData().getAlramSpeed()+"");
                                tvJishache.setText(info.getData().getAlramBrake()+"");
                                tvChaosu.setText(info.getData().getOverSpeed()+"");
                                tvLicheng.setText(info.getData().getTripMileage()+"");
                                tvShichang.setText(info.getData().getLongTime()+"");
                                tvYouhao.setText(info.getData().getFuelConsumption()+"");
                                if (info.getData().getScore()==100){
                                    driveBehaviorCheshou.setText("5星车手，击败了全国99%的车手");
                                }else if (info.getData().getScore()>90&&info.getData().getScore()<100){
                                    driveBehaviorCheshou.setText("4星车手，击败了全国90%的车手");
                                }else {
                                    driveBehaviorCheshou.setText("3星车手，击败了全国80%的车手");
                                }
                                driveBehaviorChepai.setText(info.getData().getCarNum());
                            }
                        }
                    }
                });
    }
}
